var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    plain: false,
    disabled: false,//按钮一旦点击提交 则不能操作

    ycid:'',
    zpmc: '',//作品名称
    content: '',//发布作品内容
    imgs: [],
    upurls: [],
    usid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var usid = wx.getStorageSync('usid');
    if (usid == "") {
      wx.redirectTo({
        url: '../../pages/index/index'
      })
    } else {
      this.setData({
        usid: usid,
        ycid: options.ycid
      })
      wx.showLoading({
        title: '请等待',
      })
      this.getxsycDetail();
    }
  },

  zpmcinput(e) {
    this.setData({
      zpmc: e.detail.value
    })
  },

  contentinput(e) {
    this.setData({
      content: e.detail.value
    })
  },


  //上传图片
  uploadimage: function () {
    var that = this;
    wx.chooseImage({
      count: 4, //张数， 默认9
      sizeType: ['compressed'], //建议压缩图
      sourceType: ['album', 'camera'], // 来源是相册、相机
      success: function (res) {
        for (var i = 0; i < res.tempFilePaths.length; i++) {
          that.imgSecCheck(res.tempFilePaths[i]);
        }
      }
    });
  },



  //.................图片检测........................
  imgSecCheck (imgitem) {
    var that=this;
    wx.showLoading({
      title: '图片检测中',
    })
    const uploadTask = app.wxupload({
      url: 'upload/imgSecCheck.html',
      filePath: imgitem,
      name: 'file',
      formData: {},
      success: function (res) {
        var datas = JSON.parse(res.data);
        wx.hideLoading();
        if(!datas.result){
          wx.showModal({
            title: '失败',
            content:datas.msg,
            showCancel: !1,
          });
          return;
        }else{
          var imgs = that.data.imgs;
          var img={
            picurl: imgitem,
            type: 1
          }
          imgs.push(img);
          that.setData({
            imgs: imgs
          })
        }
      }
    })
  },

  //删除图片
  delImage: function (e) {
    var that = this;
    var imgs = that.data.imgs;
    var cuindex = e.target.dataset.index;
    imgs.splice(cuindex, 1);
    that.setData({
      imgs: imgs
    })
  },

  //预览图片
  viewimage: function (e) {
    var cuindex = e.target.dataset.index;
    var that = this;
    var imgs = that.data.imgs;
    var urls =[];
    for (var i = 0; i < imgs.length;i++){
      if (imgs[i].type==0){
        urls.push(imgs[i].picurl);
      }else{
        urls.push(imgs[i].picurl);
      }
    }
    wx.previewImage({
      current: urls[cuindex], //当前显示图片的http链接
      urls: urls //需要预览的图片http链接列表
    })
  },



  sureBookxsyc: function (e) {
    var that = this;
    var zpmc = that.data.zpmc;
    if (zpmc == '') {
      wx.showModal({
        title: '友情提示',
        content: '请填写作品名称!',
        showCancel: !1,
      })
      return;
    }

    var content = that.data.content;
    var imgs = this.data.imgs;
    if (content == '' && imgs.length <= 0) {
      wx.showModal({
        title: '友情提示',
        content: '原创作品内容与文章图片不能都为空，请填写作品内容或上传文章图片!',
        showCancel: !1,
      })
      return;
    }
    wx.showLoading({
      title: '保存中',
    })
    that.setData({
      disabled: true,
      loading: true
    })

    //先上传图片
    if (imgs.length > 0) {
      //先上传图片
      that.uploadBookurl();
    } else {
      that.saveBookxsyc();
    }
  },



  //提交原创作品图片
  uploadBookurl: function () {
    var that = this;
    var imgs = this.data.imgs;
    var upurls = this.data.upurls;
    var imgg = imgs[0];
    if (imgg.type==0){
      upurls.push(imgg.picurl);
      that.setData({
        upurls: upurls
      });
      that.data.imgs.splice(0, 1);
      if (that.data.imgs.length > 0) {
        that.uploadBookurl();
      } else {
        that.saveBookxsyc();
      }
    }else{
      const uploadTask = app.wxupload({
        url: 'upload/uploadwx.html?savefile=xsyc',
        filePath: imgg.picurl,
        name: 'file',
        formData: {},
        success: function (res) {
          var img = JSON.parse(res.data);
          var url = img.urls;
          upurls.push(url);
          that.setData({
            upurls: upurls
          });
          that.data.imgs.splice(0, 1);
          if (that.data.imgs.length > 0) {
            that.uploadBookurl();
          } else {
            that.saveBookxsyc();
          }
        }
      })
    }
  },


  saveBookxsyc() {
    var that = this;
    var ycid = that.data.ycid;
    var zpmc = that.data.zpmc;
    var content = that.data.content;
    var bstate ="1";
    var upurls = that.data.upurls;

    app.wxpost({
      url: 'wxsyc/updateBookxsyc.html',
      data: {
        ycid: ycid,
        zpmc: zpmc,
        content: content,
        bstate: bstate,
        upurls: JSON.stringify(upurls)
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          disabled: false,
          loading: false
        });
    
        if (res.data.result) {
          wx.showModal({
            title: '成功',
            content: res.data.msg,
            showCancel: !1,
          });
          wx.redirectTo({
            url: '../../pages/xsyc/xsyc'
          })
        } else {
          wx.showModal({
            title: '失败',
            content: res.data.msg,
            showCancel: !1,
          });
        }
      }
    })
  },


  getxsycDetail(e) {
    var that = this;
    var ycid = that.data.ycid;
    app.wxget({
      url: 'wxsyc/getxsycDetail.html',
      data: {
        ycid: ycid
      },
      success: function (res) {
        var xsyc = res.data;
        var pics = xsyc.pics;
        var imgs=[];
        for (var i = 0; i < pics.length;i++){
          var pic={
            picurl: pics[i].PICURL,
            type:0
          }
          imgs.push(pic);
        }
        var zpmc = '';
        if (xsyc.ZPMC){
          zpmc = xsyc.ZPMC;
        }
        var content = '';
        if (xsyc.CONTENT) {
          content = xsyc.CONTENT;
        }
        that.setData({
          zpmc: zpmc,
          content: content,
          imgs: imgs
        });
        wx.setNavigationBarTitle({
          title: "《" + res.data.ZPMC + "》"
        })
        wx.hideLoading()
      }
    })
  },

})